<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$setting=plugin('jy_merch')->plugin_setting('setting');
if($_RQ['wxgid']){
	$id=pdo_getcolumn('core_account_wxapp',array('id'=>$_RQ['wxgid'],'uniacid'=>$_SESSION['uniacid']),'plugin_key');
}
$_RQ['id']=$_RQ['id']?:$_RQ['merchid'];
if($_RQ['id'] && !$id){
	$id=$_RQ['id'];
}
$id=$id?:$_SESSION['merchid'];
$mid=$_RQ['fromuser']?:$_RQ['mid'];
$mid=$mid?:$_SESSION['uid'];
$return['params']=array();
if($_RQ['op']=='main'){
	if($id){
		$myshop=pdo_get('jy_merch_user',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}elseif($mid){
		$myshop=pdo_get('jy_merch_user',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
	}else{
		exi('无法访问小店，没有小店ID','error');
	}
	$_SESSION['merchid']=$myshop['id'];
	model('jy_merch/member')->addmember($myshop['id']);
	$return['pluginset']['merchfooternav']=model('jy_merch/diy')->merchfooternav($myshop['id'],$return['pluginset']);
	if($_RQ['orderid']){
		$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>0,'id'=>$_RQ['orderid']),array('id','status','tableid'));
	}
	//凭号下单
	$return['tableid']=0;
	$tableid = intval($_RQ['tableid'])?:$order['tableid'];
	if($tableid){
		$this->session('tableid',$tableid);
	}else{
		$tableid=$this->session('tableid');
	}
	if($tableid){
		$table = pdo_get('jy_weishop_table',array('merchid'=>$myshop['id'],'uniacid'=>$_SESSION['uniacid'],'id'=>$tableid));
		$this->session('tableid',$tableid);
		$return['tableid']=$tableid;
		if(!$order['id']){
			$order=pdo_get('jy_weishop_order',array('tableid'=>$tableid,'uniacid'=>$_SESSION['uniacid'],'status'=>0,'createtime >'=>TIMESTAMP-3*3600),array('id','status','tableid'));
			if($order['id']){
				//如果清台了，订单不算
				if(!$table['busy']){
					$order=array();
				}
			}
		}
	}
	$return['oldorderid']=$order['id'];
	$this->session('oldorderid',$return['oldorderid']);
	$return['order']=$order;
	$usernum = intval($_RQ['usernum']);
	if($usernum){
		$this->session('usernum',$usernum);
		$this->session('childnum',intval($_RQ['usernum']));
	}
	$usernum=$this->session('usernum');
	if(!$usernum && $tableid && !$return['oldorderid'] && !$table['usernum']){
		exi('','','jy_merchcmi/getqueue//from=table&merchid='.$myshop['id']);
	}
	if($tableid){
		//不是我在用
		if($table['nowuserid'] !=$_SESSION['uid']){
			if($table['status'] <1){
				$this->session('tableid','');
				exi('不好意思，该餐桌未开放，请选择其他餐桌！','error','jy_merchcmi/index//id='.$myshop['id']);
			}
			if($table['hebingid'] && $table['busy']==4){
				exi('餐桌已被合并，请让主餐桌扫码点餐!','error','jy_merchcmi/index//id='.$myshop['id']);
			}
		}
		if(!$table['busy']){
			pdo_update('jy_weishop_table',array('usernum'=>$usernum,'childnum'=>$this->session('childnum'),'up_time'=>time(),'is_get'=>0,'scanningnum +'=>1,'busy'=>1,'nowuserid'=>$_SESSION['uid']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$tableid));
		}
	}
	$return['table']=$table;
	//如果场景体自定义了店铺页面
	if($_SESSION['current_plugin'] && $_SESSION['current_plugin']!='core' && $_SESSION['current_plugin']!='jy_weishop'){
			$success_url=plugin($_SESSION['current_plugin'])->merch_shop;
			if($success_url){
				exi('','',$_SESSION['current_plugin'].$success_url.'/id='.$_RQ['id'].'&tableid='.$tableid.'&usernum='.$usernum);
			}
	}
	model('jy_weishop/member')->checklogin('','','',$myshop['id']);
	if($myshop['status']!=1){
		exi('该小店暂未上线！','error','jy_merchcmi/index/');
	}
	model('jy_merch/member')->up_catefans($myshop['id']);
	model('jy_merch/member')->up_addmember($myshop['id']);
	$return['merchmember']=pdo_get('jy_merch_member',array('merchid'=>$myshop['id'],'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	if(!$return['merchmember']['mobile']){
		$return['show_setmobile2']=1;
	}
	$return['member']=cfc('core')->member_show($_SESSION['uid']);
	$this->session('merchid',$myshop['id']);
	if($myshop['id']){
		$_SESSION['merchid']=$myshop['id'];
	}
	$goodsid=intval($_RQ['goodsid']);
	$return['fromgoods']=array();
	if($goodsid){
		$fields=array('id','title','subtitle','image','video','price','active_plugin','active_id','salenum','pluginname','originalprice','total','weight','unit');
		$return['fromgoods']=pdo_get('jy_weishop_goods',array('id'=>$goodsid,'uniacid'=>$_SESSION['uniacid']),$fields);
		$cartinfo=pdo_get('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$return['fromgoods']['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']));
		$cartinfo['total']=pdo_getsum('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$return['fromgoods']['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
		$return['fromgoods']['cartid']=$cartinfo['id']?:0;
		$return['fromgoods']['carttotal']=$cartinfo['total']?:0;
		$return['fromgoods']['cartprice']=$cartinfo['price']?:0;
	}
	$citypeisong=plugin('jy_weishop')->plugin_setting('citypeisong');
	$myshop['out_price']=$myshop['out_price']>0?$myshop['out_price']:$citypeisong['out_price'];
	if(!$myshop['type']){
		$myshop['out_price']=0;
	}
	$myshop['goodscount']=pdo_count('jy_weishop_goods',array('merchid'=>$myshop['id']));
	$myshop['ordercount']=pdo_count('jy_weishop_order',array('merchid'=>$myshop['id']));
	$myshop['orderfee']=pdo_getsum('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$myshop['id']),'price');
	$myshop['lat_lng']=json_decode($myshop['lat_lng'],true);
	$myshop['onlinetime']=$myshop['onlinetime']?json_decode($myshop['onlinetime'],true):array('star'=>'07:00','end'=>'22:00');
	$myshop['onlinetime']['star']=$myshop['onlinetime']['start']?:'7:00';
	$myshop['onlinetime']['end']=$myshop['onlinetime']['end']?:'22:00';	
	$days=date('Y-m-d',TIMESTAMP);
	$star=strtotime($days.' '.$myshop['onlinetime']['star']);
	$end=strtotime($days.' '.$myshop['onlinetime']['end']);
	if($citypeisong['isopen'] && !$citypeisong['is_dispatch'] && ($star>TIMESTAMP || TIMESTAMP>$end)){
		$myshop['offline']=1;
	}
	$tags=str_array($return['pluginset']['tags'],'|');
	$key=mt_rand(0,count($tags));
	$myshop['tags']=$myshop['tags']?:$tags[$key];
	$myshop['coupons']=model('jy_weishop/coupon')->getmerchcoupon($myshop['id'],1);
	$myshop['logo']=tomedia($myshop['logo']);
	$myshop['pcd']=json_decode($myshop['pcd'],true);
	$myshop['address']=$myshop['pcd']['city'].' '.$myshop['pcd']['district'].' '.$myshop['pcd']['street'].' '.$myshop['address'];
	$return['kefu']=$myshop['iskefu'];
	$return['setting']=$setting;
	$return['params']=array('page'=>1,'type'=>0);
	//补充
	$category=pdo_getall('jy_weishop_goods_group',array('merchid'=>$myshop['id'],'uniacid'=>$_SESSION['uniacid'],'isshow'=>1,'parentid'=>0),'','','paixu desc');
	if(empty($category)){
		$category=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'mid'=>0,'merchid'=>0,'isshow'=>1,'parentid'=>0),'','','paixu desc');
	}else{
		$ismygroups=true;
	}
	$getcarts=model('jy_weishop/cart')->getall(1,$myshop['id'],true);
	$carts=$getcarts['list'];
	$allnum=$getcarts['alltotal'];
	$goodsprice=$getcarts['goodsprice'];
	foreach($category as $k=>&$v){
		$allgoods=pdo_getall('jy_weishop_goods',array('merchid'=>$myshop['id'],'status'=>1,'uniacid'=>$_SESSION['uniacid'],'pcate2'=>$v['id']),'id');
		if(empty($allgoods)){
		  $allgoods=pdo_getall('jy_weishop_goods',array('merchid'=>$myshop['id'],'status'=>1,'uniacid'=>$_SESSION['uniacid'],'pcate'=>$v['id']),'id');  
		}
		$v['total']=0;
		foreach($allgoods as $ag){
			$carttotal=pdo_getcolumn('jy_weishop_member_cart',array('goodsid'=>$ag['id'],'selected'=>1,'mid'=> $_SESSION['uid'],'uniacid' => $_SESSION['uniacid']),'total');
			$v['total'] +=$carttotal;
		}
		if(empty($allgoods)){
			unset($category[$k]);
		}
	}
	//运费计算：
	$yunorder=array(
		'list'=>$carts,
		'total_price'=>$goodsprice,
	);
	$up_dispatch=model('jy_weishop/order/dispatch')->up_dispatch($yunorder,array(),'',$_SESSION['store_id'],$myshop);
	$return['dispatchprice']=$up_dispatch['get_freight']['fee'];
	$allprice = $goodsprice - $youhui;
	$return['category']=$category;
	$return['nowckey']=0;
	$return['allnum']=$allnum;
	$return['totalprice']=$allprice;
	$return['carts']=$carts;
	$return['showlist']='goods';
	if($setting['is_bigwheel'] && $this->plugin_exists('jy_bigwheel')){
		$return['bigwheelgames']=model('jy_bigwheel/model')->merchgame($myshop['id']);
	}
	$return['params']['pcate']='all';
	$sharelink='jy_merchcmi/myshop//id='.$myshop['id'].'&fromuser='.$_SESSION['uid'];
	$return['sharedata']=array(
			'title'=>$myshop['merchname'],
			'desc'=>$myshop['desc']?$myshop['desc']:$myshop['merchname'].'的小店，欢迎光临！',
			'link'=>custom_url($sharelink,true),
			'imgUrl'=>tomedia($myshop['logo'])
	);
	$return['myshop']=$myshop;
	$return['islike']=pdo_getcolumn('jy_merch_like_log',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'merchid'=>$myshop['id']),'islike');
	set_html('title',$myshop['merchname']);
	exi($return);
}
if($_RQ['op']=='groupslist'){
	$merchid=intval($_RQ['merchid']);
	$merchid=$merchid?:$this->session('merchid');
	$params=array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'isshow'=>1,'parentid'=>0);
	if($_RQ['pcate']){
		if($_RQ['pcate']!='all'){
			$params['id']=$_RQ['pcate'];
		}
	}
	$pcate=intval($_RQ['pcate']);
	$category=pdo_getall('jy_weishop_goods_group',$params,array('id','title'),'id','paixu desc');
	if(empty($category)){
		$category=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'mid'=>0,'merchid'=>0,'isshow'=>1,'parentid'=>0),array('id','title'),'id','paixu desc');
	}
	$params=array('uniacid'=>$_SESSION['uniacid'],'type <>'=>4,'status'=>1,'nochecked'=>0,'merchid'=>$merchid);
	if(trim($_RQ['keyword'])){
			$params['title like']='%'.trim($_RQ['keyword']).'%';
			$return['params']['keyword']=trim($_RQ['keyword']);
	}
	$total='';
	$fields=array('id','title','subtitle','image','video','price','pcate','pcate2','active_plugin','active_id','salenum','pluginname','originalprice','total','weight','unit');
	$orderby='paixu desc ,salenum desc , id desc ';
	if($_RQ['order']){
			if($_RQ['order']=='sales' || $_RQ['order']==0){
				$orderby='salerealnum desc';
			}
			if($_RQ['order']==1){
				$params['saletype likes']=array('1');
			}
			if($_RQ['order']==2){
				$params['saletype likes']=array('3');
			}
			if($_RQ['order']=='price'){
				if($_RQ['order_type']==2){
					$orderby='price desc';
				}else{
					$orderby='price asc';
				}
			}
	}
	$list=array();
	foreach($category as &$itemv){
		$params['pcate2']=intval($itemv['id']);
		$parameter=array(
			'params'=>$params,
			'limit'=>array(1,1000),
			'fields'=>$fields,
			'orderby'=>$orderby,
		);
		$goods=model('jy_weishop/goods')->goods_list($parameter,$total);
		if(!$goods){
			unset($parameter['params']['pcate2']);
			$parameter['params']['pcate']=intval($itemv['id']);
			$goods=model('jy_weishop/goods')->goods_list($parameter,$total);
		}
		if($goods){
			$goods=model('jy_merch/goods')->uplist($goods);
			if($goods[0]){
				$goods[0]['grouptitle']=$itemv['title'];
			}
			foreach($goods as $gv){
				$list[]=$gv;
			}
		}
		if($_RQ['isgroup']){
			$itemv['goodslist']=$goods;
		}
	}
	if($_RQ['isgroup']){
		if($_RQ['page']>1){
			exi('none');
		}
		$category=array_values($category);
		exi($category);
	}else{
		$page = intval($_RQ['page']);
		$page=$page?:1;
		$start=($page-1)*500;
		$list=array_slice($list,$start,500);
		if(!$list){
			exi('none');
		}
		exi($list);
	}
}
if($_RQ['op']=='getcomments'){
	$merchid=intval($_RQ['merchid']);
	$merchid=$merchid?:$this->session('merchid');
	$return['list']=model('jy_weishop/goods')->get_comments(0,6,$comment_total,$_RQ['type'],$_RQ['page'],$merchid);
	exi($return);
}
if($_RQ['op']=='get_juli'){
	if($_RQ['lat'] || $_RQ['lng']){
		$lat_lng=array('lat'=>$_RQ['lat'],'lng'=>$_RQ['lng']);
	}
	$merchid=intval($_RQ['merchid'])?:$this->session('merchid');
	$storelat_lng=pdo_getcolumn('jy_merch_user',array('id'=>$merchid),'lat_lng');
	$storelat_lng=json_decode($storelat_lng,true);
	$juli=cfc('tools')->getdistance($lat_lng['lng'],$lat_lng['lat'],$storelat_lng['lng'],$storelat_lng['lat']);
	$juli=intval($juli*100)/100000;
	$juli=$juli?:0;
	exi($juli);
}
if($_RQ['op']=='check_buy'){
	$merchid=intval($_RQ['merchid']);
	$sendtype=intval($_RQ['sendtype']);
	$tableid=intval($_RQ['tableid']);
	$oldorderid=$this->session('oldorderid');
	$prams=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'selected'=>1);
	if($merchid){
		if($merchid==-1){
			$prams['merchid']=0;
		}else{
			$prams['merchid']=$merchid;
		}
	}
	$cart_list=pdo_getall('jy_weishop_member_cart',$prams,array('id','goodsid','total','optionid','merchid'));
	if(!$cart_list && !$oldorderid){
		exi('您的购物车没有选中商品！','error');
	}
	model('jy_weishop/cart')->check_buy($cart_list);
	model('jy_weishop/activity/sales')->check_sale_new($cart_list);
	$query='sendtype='.$sendtype.'&merchid='.$merchid.'&tableid='.$tableid.'&id='.$oldorderid;
	if($oldorderid){
		$query .='&goodslist=1';
	}
	$resurl='jy_merchcmi/order.check//'.$query;
	if($_RQ['isresurl']){
		exi($resurl);
	}else{
		exi('','',$resurl);
	}
}
if($_RQ['op']=='like'){
	$postdata=get_postdata();
	$merchid=$postdata['merchid'];
	if(!$merchid){
		exi('没有找到该商户，请重新选择！','error');
	}
	$looklist=pdo_get('jy_merch_like_log',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'merchid'=>$merchid));
	if($looklist){
		if($looklist['islike'] !=1){
			pdo_update('jy_merch_user',array('likesnum +='=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$merchid));
			pdo_update('jy_merch_like_log',array('islike'=>1),array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
			exi('收藏成功！');
		}else{
			pdo_update('jy_merch_user',array('likesnum -='=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$merchid));
			pdo_update('jy_merch_like_log',array('islike'=>0),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'merchid'=>$merchid));
			exi('取消收藏成功！');
		}
	}else{
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'merchid'=>$merchid,
			'mid'=>$_SESSION['uid'],
			'islike'=>1,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_merch_like_log',$insert);
		exi('收藏成功！');
	}
}
if($_RQ['op']=='up_fans'){
	$postdata=get_postdata();
	if($_RQ['isfail']){
		$_SESSION['isshow_fansxieyi']=1;
		exi('取消成功！');
	}
	model('jy_merch/member')->up_catefans($postdata['storeid']);
	model('jy_merch/member')->up_addmember($postdata['storeid']);
	exi('绑定成功！');
}
?>